package com.probox.common.entity.service.wechat.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.WechatLiveStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.wechat.api.domain.WechatBroadcastVo;
import com.probox.wechat.api.domain.WchatBroadcastVo;
import com.probox.common.entity.service.wechat.IWchatBroadcastService;
import com.probox.common.entity.domain.wechat.vo.ResponseWechatBroadcastVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.wechat.WchatBroadcastMapper;
import com.probox.common.entity.domain.wechat.broadcast.WechatBroadcast;
import org.springframework.transaction.annotation.Transactional;

/**
 * 小程序直播间Service业务层处理
 *
 * @author usoft
 * @date 2021-07-26
 */
@Service
@Transactional
public class WchatBroadcastServiceImpl extends ServiceImpl<WchatBroadcastMapper, WechatBroadcast> implements IWchatBroadcastService {
    @Autowired
    private WchatBroadcastMapper wchatBroadcastMapper;

    /**
     * 查询小程序直播间
     *
     * @param broadcastId 小程序直播间ID
     * @return 小程序直播间
     */
    @Override
    public WechatBroadcast selectWchatBroadcastById(Long broadcastId) {
        return this.getById(broadcastId);
    }

    /**
     * 查询小程序直播间列表
     *
     * @param wechatBroadcast 小程序直播间
     * @return 小程序直播间
     */
    @Override
    public List<WechatBroadcast> queryList(WechatBroadcast wechatBroadcast) {
        QueryWrapper<WechatBroadcast> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getBroadcastId())) && wechatBroadcast.getBroadcastId() != null) {
            queryWrapper.eq("broadcast_id", wechatBroadcast.getBroadcastId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getRoomId())) && wechatBroadcast.getRoomId() != null) {
            queryWrapper.eq("id", wechatBroadcast.getRoomId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getName())) && wechatBroadcast.getName() != null) {
            queryWrapper.eq("name", wechatBroadcast.getName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getCoverImg())) && wechatBroadcast.getCoverImg() != null) {
            queryWrapper.eq("cover_img", wechatBroadcast.getCoverImg());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getStartTime())) && wechatBroadcast.getStartTime() != null) {
            queryWrapper.eq("start_time", wechatBroadcast.getStartTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getEndTime())) && wechatBroadcast.getEndTime() != null) {
            queryWrapper.eq("end_time", wechatBroadcast.getEndTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getAnchorName())) && wechatBroadcast.getAnchorName() != null) {
            queryWrapper.eq("anchor_name", wechatBroadcast.getAnchorName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getAnchorWechat())) && wechatBroadcast.getAnchorWechat() != null) {
            queryWrapper.eq("anchor_wechat", wechatBroadcast.getAnchorWechat());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getShareImg())) && wechatBroadcast.getShareImg() != null) {
            queryWrapper.eq("share_img", wechatBroadcast.getShareImg());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getFeedsImg())) && wechatBroadcast.getFeedsImg() != null) {
            queryWrapper.eq("feeds_img", wechatBroadcast.getFeedsImg());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getType())) && wechatBroadcast.getType() != null) {
            queryWrapper.eq("type", wechatBroadcast.getType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getCloseLike())) && wechatBroadcast.getCloseLike() != null) {
            queryWrapper.eq("close_like", wechatBroadcast.getCloseLike());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getCloseGoods())) && wechatBroadcast.getCloseGoods() != null) {
            queryWrapper.eq("close_goods", wechatBroadcast.getCloseGoods());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getCloseComment())) && wechatBroadcast.getCloseComment() != null) {
            queryWrapper.eq("close_comment", wechatBroadcast.getCloseComment());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getCreateUser())) && wechatBroadcast.getCreateUser() != null) {
            queryWrapper.eq("CREATE_USER", wechatBroadcast.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getCreateName())) && wechatBroadcast.getCreateName() != null) {
            queryWrapper.eq("CREATE_NAME", wechatBroadcast.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getUpdateUser())) && wechatBroadcast.getUpdateUser() != null) {
            queryWrapper.eq("UPDATE_USER", wechatBroadcast.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(wechatBroadcast.getUpdateName())) && wechatBroadcast.getUpdateName() != null) {
            queryWrapper.eq("UPDATE_NAME", wechatBroadcast.getUpdateName());
        }
        return wchatBroadcastMapper.selectList(queryWrapper);
    }



    /**
     * 新增小程序直播间
     *
     * @param wechatBroadcast 小程序直播间
     * @return 结果
     */
    @Override
    public boolean insertWchatBroadcast(WechatBroadcast wechatBroadcast) {
        DateTime date = DateUtil.date();
        Long platformId = SecurityUtils.getPlatformId();
        String username = SecurityUtils.getUsername();
        Long userId = SecurityUtils.getUserId();

        wechatBroadcast.setDataStatus(DataStatus.valid.getKey());

        wechatBroadcast.setPlatformUid(platformId);

        wechatBroadcast.setCreateUser(userId);
        wechatBroadcast.setCreateName(username);
        wechatBroadcast.setCreateTime(date);

        wechatBroadcast.setUpdateUser(userId);
        wechatBroadcast.setUpdateName(username);
        wechatBroadcast.setUpdateTime(date);

        return this.save(wechatBroadcast);

    }

    /**
     * 修改小程序直播间
     *
     * @param wechatBroadcast 小程序直播间
     * @return 结果
     */
    @Override
    public boolean updateWchatBroadcast(WechatBroadcast wechatBroadcast) {
        DateTime date = DateUtil.date();
        String username = SecurityUtils.getUsername();
        Long userId = SecurityUtils.getUserId();

        wechatBroadcast.setUpdateUser(userId);
        wechatBroadcast.setUpdateName(username);
        wechatBroadcast.setUpdateTime(date);

        return this.updateById(wechatBroadcast);
    }

    /**
     * 批量删除小程序直播间
     *
     * @param broadcastIds 需要删除的小程序直播间ID
     * @return 结果
     */
    @Override
    public boolean deleteWchatBroadcastByIds(Long[] broadcastIds) {
        return this.removeByIds(Arrays.asList(broadcastIds));
    }


    //--------------------对外接口

    /**
     * 小程序获取直播间列表接口
     * @param name
     * @return
     */
    @Override
    public List<ResponseWechatBroadcastVo> getWchatBroadcast(String name) {
        QueryWrapper<WechatBroadcast> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(name)) && name != null) {
            queryWrapper.like("name", name);
        }
        queryWrapper.ne("DATA_STATUS",DataStatus.delete.getKey());
        queryWrapper.eq("PLATFORM_UID",SecurityUtils.getCustomerPlatformUid());
        List<WechatBroadcast> wechatBroadcasts = wchatBroadcastMapper.selectList(queryWrapper);
        List<ResponseWechatBroadcastVo> collect = wechatBroadcasts.stream().map(o -> {
            ResponseWechatBroadcastVo vo = new ResponseWechatBroadcastVo();
            BeanUtils.copyProperties(o,vo);
            Date date = DateUtil.date();
            if(date.before(o.getStartTime())){
                vo.setLiveStatus(WechatLiveStatus.no_started.getKey());
                vo.setLiveStatusStr(WechatLiveStatus.no_started.getValue());
            }else if(date.after(o.getEndTime())){
                vo.setLiveStatus(WechatLiveStatus.past.getKey());
                vo.setLiveStatusStr(WechatLiveStatus.past.getValue());
            }else {
                vo.setLiveStatus(WechatLiveStatus.finished.getKey());
                vo.setLiveStatusStr(WechatLiveStatus.finished.getValue());
            }
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 统计
     * @param name
     * @return
     */
    @Override
    public Integer getWchatBroadcastTotal(String name) {
        QueryWrapper<WechatBroadcast> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(name)) && name != null) {
            queryWrapper.like("name", name);
        }
        queryWrapper.ne("DATA_STATUS",DataStatus.delete.getKey());
        queryWrapper.eq("PLATFORM_UID",SecurityUtils.getCustomerPlatformUid());
        return wchatBroadcastMapper.selectCount(queryWrapper);
    }


    //----------------------远程调用接口
    /**
     * 远程调用获取获取直播间
     * @param product
     * @return
     */
    @Override
    public WchatBroadcastVo getBroadcast(String product) {
        WechatBroadcast wechatBroadcast = wchatBroadcastMapper.selectBroadcastByProduct(product);
        if (wechatBroadcast == null){
            return null;
        }
        WchatBroadcastVo wchatBroadcastVo = new WchatBroadcastVo();
        BeanUtils.copyProperties(wechatBroadcast,wchatBroadcastVo);
        return  wchatBroadcastVo;
    }

    /**
     * 远程调用获取获取直播间
     * @return
     */
    @Override
    public WechatBroadcastVo getBroadcastById(Long rooId,Long platformId) {

        QueryWrapper<WechatBroadcast> wechatBroadcastQueryWrapper = new QueryWrapper<>();
        wechatBroadcastQueryWrapper.eq("PLATFORM_UID", platformId);
        wechatBroadcastQueryWrapper.eq("ROOM_ID",rooId);
        wechatBroadcastQueryWrapper.eq("DATA_STATUS",DataStatus.valid.getKey());
        WechatBroadcast wechatBroadcast = wchatBroadcastMapper.selectOne(wechatBroadcastQueryWrapper);
        if (wechatBroadcast == null){
            return null;
        }
        WechatBroadcastVo wechatBroadcastVo = new WechatBroadcastVo();
        if (wechatBroadcast != null){
            BeanUtils.copyProperties(wechatBroadcast,wechatBroadcastVo);
        }
        Date date = DateUtil.date();
        if(date.before(wechatBroadcast.getStartTime())){
            wechatBroadcastVo.setLiveStatus(WechatLiveStatus.no_started.getKey());
            wechatBroadcastVo.setLiveStatusStr(WechatLiveStatus.no_started.getValue());
        }else if(date.after(wechatBroadcast.getEndTime())){
            wechatBroadcastVo.setLiveStatus(WechatLiveStatus.past.getKey());
            wechatBroadcastVo.setLiveStatusStr(WechatLiveStatus.past.getValue());
        }else {
            wechatBroadcastVo.setLiveStatus(WechatLiveStatus.finished.getKey());
            wechatBroadcastVo.setLiveStatusStr(WechatLiveStatus.finished.getValue());
        }

        return  wechatBroadcastVo;
    }
}
